from bs4 import BeautifulSoup
import requests
from urllib.request import urlopen
import os
# url = 'http://www.ngchina.com.cn/animals/'
# http = requests.get(url).text      这个解码网址没有解码出中文

htp = urlopen('http://www.ngchina.com.cn/animals/').read().decode('utf_8') # 解码网页
soup = BeautifulSoup(htp,'lxml')
img_ul = soup.find_all('ul',{'class':'img_list'})
print(len(img_ul))

#os.makedirs('./img/',exist_ok=True)  # 创建一个文件夹

for i in img_ul:
    imgs = i.find_all('img')
    for img in imgs:
        url = img['src']
        r = requests.get(url,stream=True)
        image_name = url.split('/')[-1]
        with open('./img/%s' % image_name,'wb') as f:
            for pho in r.iter_content(chunk_size=128):
                f.write(pho)
    #for q in imgs.find_all('div')
